<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="内部支领单编号" prop="issueCode">
        <el-input
          v-model="queryParams.issueCode"
          placeholder="请输入内部支领单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="内部支领单名称" prop="issueName">
        <el-input
          v-model="queryParams.issueName"
          placeholder="请输入内部支领单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支领人" prop="supportName">
        <el-input
          v-model="queryParams.supportName"
          placeholder="请输入支领人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出库日期" prop="params">
        <el-date-picker
          clearable
          v-model="queryParams.params"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          placeholder="请选择出库日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>
    <div style="height: 15px"></div>
    <el-card style="border-radius: 10px">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="issueheaderotherList" @selection-change="handleSelectionChange">
      <el-table-column label="内部支领单编号" align="center"  prop="issueCode">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['wm:issueinternal:query']"
          >{{ scope.row.issueCode }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="内部支领单名称" align="center" prop="issueName" :show-overflow-tooltip="true"/>

      <el-table-column label="支领人" align="center" prop="supportName" :show-overflow-tooltip="true"/>
      <el-table-column label="支领部门" align="center" prop="supportDeptName" :show-overflow-tooltip="true"/>
      <el-table-column label="支领日期" align="center" prop="issueDate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.issueDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
      <el-table-column label="单据状态" align="center" prop="status" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" width="180">
        <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-video-play"
              @click="handleExecute(scope.row)"
              v-if="scope.row.status =='APPROVED'"
              v-hasPermi="['wm:issueinternal:execute']"
            >执行出库</el-button>

            <el-button
              size="mini"
              type="text"
              icon="el-icon-circle-check"
              @click="handleConfirme(scope.row)"
              v-if="scope.row.status =='PREPARE'|| scope.row.status =='DISALLOW'"
              v-hasPermi="['wm:issueinternal:edit']"
            >提交</el-button>
            <el-button
                size="mini"
                type="text"
                icon="el-icon-s-check"
                @click="toApprove(scope.row)"
                v-if="scope.row.status =='CONFIRMED'"
                v-hasPermi="['wm:issueinternal:approve']"
            >审核</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-if="scope.row.status =='PREPARE' || scope.row.status =='DISALLOW'"
              v-hasPermi="['wm:issueinternal:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-if="scope.row.status =='PREPARE' || scope.row.status =='CONFIRMED' || scope.row.status =='DISALLOW'"
              v-hasPermi="['wm:issueinternal:remove']"
            >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>
    <!-- 添加或修改内部支领单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="内部支领单编号" prop="issueCode">
              <el-input v-model="form.issueCode" placeholder="请输入内部支领单编号" :readonly="optType !== 'add'"/>
            </el-form-item>
          </el-col>
<!--          <el-col :span="4">
            <el-form-item label-width="80">
              <el-switch v-model="autoGenFlag"
                         active-color="#13ce66"
                         active-text="自动生成"
                         @change="handleAutoGenChange(autoGenFlag)" v-if="optType != 'view' && form.status =='PREPARE'">
              </el-switch>
            </el-form-item>
          </el-col>-->
          <el-col :span="12">
            <el-form-item label="内部支领单名称" prop="issueName">
              <el-input v-model="form.issueName" placeholder="请输入内部支领单名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="支领日期" prop="issueDate">
              <el-date-picker clearable
                              v-model="form.issueDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择支领日期"
                              :default-value="new Date()"
                              style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="支领人" prop="supportName">
              <el-input v-model="form.supportName" readonly placeholder="请选择支领人">
                <el-button slot="append" @click="handleUserSelect" icon="el-icon-search"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支领部门" prop="supportDeptName">
              <el-input v-model="form.supportDeptName" :disabled="true" placeholder="请选择支领部门"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请填写出库原因"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-divider v-if="form.issueId !=null" content-position="center">物料信息</el-divider>
      <el-card shadow="always" v-if="form.issueId !=null" class="box-card">
        <Issueinternalline ref="line" :issueId="form.issueId" :warehouseId="form.warehouseId" :locationId="form.locationId" :areaId="form.areaId"
                   :optType="optType"></Issueinternalline>
      </el-card>
      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary" @click="cancel" v-if="optType =='view' || form.status !='PREPARE' ">返回</el-button>-->
        <el-button type="primary" @click="submitForm" v-if="optType !='view' ">确 定</el-button>
        <el-button type="primary" @click="handleApprove" v-if="form.status =='CONFIRMED' && optType =='view' ">审  核</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <UserSingleSelect ref="userSelect" @onSelected="onUserSelected"></UserSingleSelect>
  </div>
</template>

<script>
import {
  addIssueInternal,
  confirme,
  delIssueInternal,
  execute,
  getIssueInternal,
  handleApprove,
  listIssueInternal,
  updateIssueInternal
} from "@/api/mes/wm/issueinternal";
import Issueinternalline from "@/views/mes/wm/issueinternal/line";
import { genCode } from "@/api/system/autocode/rule";
import ClientSelect from "@/components/clientSelect/single.vue";
import UserSingleSelect from "@/components/userSelect/single.vue"


export default {
  name: "Other",
  dicts: ['mes_order_status'],
  components: {UserSingleSelect, ClientSelect, Issueinternalline},
  data() {
    return {
      autoGenFlag: false,
      optType: undefined,
      warehouseInfo: [],
      warehouseOptions: [],
      warehouseProps: {
        multiple: false,
        value: 'pId',
        label: 'pName',
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 内部支领单表格数据
      issueheaderotherList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        issueCode: null,
        issueName: null,
        clientId: null,
        clientCode: null,
        clientName: null,
        clientNick: null,
        warehouseId: null,
        warehouseCode: null,
        warehouseName: null,
        locationId: null,
        locationCode: null,
        locationName: null,
        areaId: null,
        areaCode: null,
        areaName: null,
        issueDate: null,
        status: null,
        params: [],
        supportName:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        issueCode: [
          {required: true, message: "内部支领单编号不能为空", trigger: "blur"}
        ],

        issueDate: [
          {required: true, message: "支领日期不能为空", trigger: "blur"}
        ],
        issueName: [
          {required: true, message: "内部支领单名称不能为空", trigger: "blur"}
        ],
        remark: [
          {required: true, message: "备注不能为空", trigger: "blur"}
        ],
        supportName: [
          {required: true, message: "支领人不能为空", trigger: "blur"}
        ],
        supportDeptName: [
          {required: true, message: "支领部门不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询内部支领单列表 */
    getList() {
      this.loading = true;
      const query = { ...this.queryParams };
      if (query.params && query.params.length === 2) {
        query.params = {
          beginTime: query.params[0],
          endTime: query.params[1]
        };
      } else {
        query.params = null;
      }
      listIssueInternal(query).then(response => {
        this.issueheaderotherList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        issueId: null,
        issueCode: null,
        issueName: null,
        workstationId: null,
        workstationCode: null,
        workstationName: null,
        workorderId: null,
        workorderCode: null,
        taskId: null,
        taskCode: null,
        clientId: null,
        clientCode: null,
        clientName: null,
        clientNick: null,
        warehouseId: null,
        warehouseCode: null,
        warehouseName: null,
        locationId: null,
        locationCode: null,
        locationName: null,
        areaId: null,
        areaCode: null,
        areaName: null,
        issueDate:  new Date().toISOString().split('T')[0],
        status: 'PREPARE',
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        supportName: null,
        supportId: null,
        supportCode: null,
        supportDeptId: null,
        supportDeptName: null,
      };
      this.autoGenFlag = false;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.issueId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      genCode('ISSUE_INTERNAL_CODE').then(response => {
        this.form.issueCode = response;
      });
      this.open = true;
      this.title = "添加内部支领单";
      this.optType = "add";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const issueId = row.issueId || this.ids
      getIssueInternal(issueId).then(response => {
        this.form = response.data;
        this.warehouseInfo[ 0 ] = response.data.warehouseId;
        this.warehouseInfo[ 1 ] = response.data.locationId;
        this.warehouseInfo[ 2 ] = response.data.areaId;
        this.open = true;
        this.title = "修改内部支领单";
        this.optType = "edit";
      });
    },
    //确认
    handleConfirme(row) {
      const issueIds = row.issueId || this.ids;
      this.$modal.confirm('确认提交内部支领单？').then(function () {
        return confirme(issueIds)//执行入库
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("确认成功");
      }).catch(() => {
      });
    },
    toApprove(row){
      this.reset();
      const issueIds = row.issueId
      getIssueInternal(issueIds).then(response => {
        this.form = response.data;
        this.warehouseInfo[ 0 ] = response.data.warehouseId;
        this.warehouseInfo[ 1 ] = response.data.locationId;
        this.warehouseInfo[ 2 ] = response.data.areaId;
        this.open = true;
        this.title = "审批内部支领单信息";
        this.optType = "view";
      });
    },
    handleApprove() {

      handleApprove(this.form.issueId).then(response => {
        this.$modal.msgSuccess("审核成功");
        this.open = false;
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs[ "form" ].validate(valid => {
        if (valid) {
          if (this.form.issueId != null) {
            updateIssueInternal(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addIssueInternal(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const issueIds = row.issueId || this.ids;
      this.$modal.confirm('是否确认删除内部支领单编号为"' + issueIds + '"的数据项？').then(function () {
        return delIssueInternal(issueIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wm/other/export', {
        ...this.queryParams
      }, `other_${new Date().getTime()}.xlsx`)
    },
    handleView(row) {
      this.reset();
      const issueIds = row.issueId
      getIssueInternal(issueIds).then(response => {
        this.form = response.data;
        this.warehouseInfo[ 0 ] = response.data.warehouseId;
        this.warehouseInfo[ 1 ] = response.data.locationId;
        this.warehouseInfo[ 2 ] = response.data.areaId;
        this.open = true;
        this.title = "查看内部支领单信息";
        this.optType = "view";
      });
    },
    //执行出库
    handleExecute(row) {
      const issueIds = row.issueId || this.ids;
      this.$modal.confirm('确认执行出库？').then(function () {
        return execute(issueIds)//执行入库
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("出库成功");
      }).catch(() => {
      });
    },
    //自动生成编码
    handleAutoGenChange(autoGenFlag) {
      if (autoGenFlag) {
        genCode('ISSUE_INTERNAL_CODE').then(response => {
          this.form.issueCode = response;
        });
      } else {
        this.form.issueCode = null;
      }
    },
    handleSelectClient() {
      this.$refs.clientSelect.showFlag = true;
    },
    //客户选择弹出框
    onClientSelected(obj) {
      if (obj != undefined && obj != null) {
        this.form.clientId = obj.clientId;
        this.form.clientCode = obj.clientCode;
        this.form.clientName = obj.clientName;
      }
    },
    //点击人员选择按钮
    handleUserSelect() {
      this.$refs.userSelect.showFlag = true;
    },
    //人员选择返回
    onUserSelected(row) {
      this.form.supportName = row.nickName;
      this.form.supportId = row.userId;
      this.form.supportCode = row.userName;
      this.form.supportDeptId = row.deptId
      this.form.supportDeptName = row.dept.deptName
      this.$forceUpdate()
    },
  }
};
</script>
